home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / ScatterGraph / ScatterGraph.frm < prev    next >
Text File  |  2001-10-08  |  33KB  |  1,188 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form GraphForm 
  4.    Caption         =   "Data Analysis Scatter Graph"
  5.    ClientHeight    =   6420
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   7875
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   12
  12.       Charset         =   0
  13.       Weight          =   400
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "ScatterGraph.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   428
  21.    ScaleMode       =   3  'Pixel
  22.    ScaleWidth      =   525
  23.    StartUpPosition =   3  'Windows Default
  24.    Begin VB.CommandButton Command1 
  25.       Caption         =   "Command1"
  26.       BeginProperty Font 
  27.          Name            =   "MS Sans Serif"
  28.          Size            =   18
  29.          Charset         =   0
  30.          Weight          =   700
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   435
  36.       Left            =   1920
  37.       TabIndex        =   0
  38.       Top             =   5820
  39.       Visible         =   0   'False
  40.       Width           =   495
  41.    End
  42.    Begin MSComDlg.CommonDialog CommonDialog1 
  43.       Left            =   1080
  44.       Top             =   5760
  45.       _ExtentX        =   847
  46.       _ExtentY        =   847
  47.       _Version        =   393216
  48.    End
  49.    Begin VB.Timer Timer1 
  50.       Enabled         =   0   'False
  51.       Interval        =   10
  52.       Left            =   240
  53.       Top             =   5760
  54.    End
  55.    Begin VB.Menu MENU_POPUP 
  56.       Caption         =   "POPUPMENU"
  57.       Visible         =   0   'False
  58.       Begin VB.Menu MENU_EXITMENU 
  59.          Caption         =   "Exit Menu!"
  60.       End
  61.       Begin VB.Menu MENU_LOAD 
  62.          Caption         =   "Load Data From File!"
  63.       End
  64.       Begin VB.Menu MENU_RESET 
  65.          Caption         =   "Reset Orientation!"
  66.       End
  67.       Begin VB.Menu MENU_CONNECT 
  68.          Caption         =   "Show connecting lines"
  69.          Checked         =   -1  'True
  70.       End
  71.       Begin VB.Menu MENU_LINES 
  72.          Caption         =   "Show height lines"
  73.          Checked         =   -1  'True
  74.       End
  75.       Begin VB.Menu MENU_FOOTLINES 
  76.          Caption         =   "Show foot lines"
  77.          Checked         =   -1  'True
  78.       End
  79.       Begin VB.Menu MENU_BASE 
  80.          Caption         =   "Show base plane"
  81.          Checked         =   -1  'True
  82.       End
  83.       Begin VB.Menu MENU_ROTATE 
  84.          Caption         =   "Auto Rotate"
  85.          Checked         =   -1  'True
  86.       End
  87.    End
  88. End
  89. Attribute VB_Name = "GraphForm"
  90. Attribute VB_GlobalNameSpace = False
  91. Attribute VB_Creatable = False
  92. Attribute VB_PredeclaredId = True
  93. Attribute VB_Exposed = False
  94.  
  95. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  96. '
  97. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  98. '
  99. '  File:       ScatterGraph.frm
  100. '  Content:    Implementation of a plot graph in 3 dimensions
  101. '
  102. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  103.  
  104. Option Explicit
  105.  
  106. Dim m_maxX As Double
  107. Dim m_minX As Double
  108. Dim m_maxY As Double
  109. Dim m_minY As Double
  110. Dim m_maxZ As Double
  111. Dim m_minZ As Double
  112. Dim m_maxsize As Double
  113. Dim m_minSize As Double
  114.  
  115. Dim m_extX As Double
  116. Dim m_extY As Double
  117. Dim m_extZ As Double
  118. Dim m_extSize As Double
  119.  
  120. Dim m_scalex As Single
  121. Dim m_scaley As Single
  122. Dim m_scalez As Single
  123. Dim m_scalesize As Single
  124.  
  125. Dim m_xHeader As String
  126. Dim m_yHeader As String
  127. Dim m_zHeader As String
  128. Dim m_sizeHeader As String
  129.  
  130.  
  131. Dim m_binit As Boolean
  132. Dim m_bGraphInit As Boolean
  133. Dim m_bMinimized As Boolean
  134.  
  135.  
  136. Dim m_graphroot As CD3DFrame
  137. Dim m_quad1 As CD3DFrame
  138. Dim m_quad2 As CD3DFrame
  139. Dim m_XZPlaneFrame As CD3DFrame
  140.  
  141. Dim m_bRot As Boolean
  142. Dim m_bHeightLines As Boolean
  143. Dim m_bConnectlines As Boolean
  144. Dim m_bShowBase As Boolean
  145. Dim m_bFootLines As Boolean
  146.  
  147. Dim m_drawtext As String
  148. Dim m_drawtextpos As RECT
  149. Dim m_drawtextEnable As Boolean
  150.  
  151. Dim m_formatX As String
  152. Dim m_formatY As String
  153. Dim m_formatZ As String
  154. Dim m_formatSize As String
  155.  
  156. Dim m_data As Collection
  157. Dim m_hwnd As Long
  158. Dim m_vbfont As IFont
  159. Dim m_vbfont2 As IFont
  160. Dim m_font2height  As Long
  161.  
  162. Dim m_lastX As Single
  163. Dim m_lasty As Single
  164. Dim m_bMouseDown As Boolean
  165.  
  166.  
  167. Dim m_Tex As Direct3DTexture8
  168.  
  169.  
  170. Dim m_LabelX As CD3DFrame
  171. Dim m_LabelY As CD3DFrame
  172. Dim m_LabelZ As CD3DFrame
  173.  
  174.  
  175. Dim m_meshobj As D3DXMesh
  176. Dim m_meshplane As D3DXMesh
  177. Dim m_font As D3DXFont
  178. Dim m_font2 As D3DXFont
  179.  
  180.  
  181.  
  182. 'Camera variables
  183. Dim m_fElapsedTime As Single
  184.  
  185. Dim m_vVelocity  As D3DVECTOR
  186. Dim m_fYawVelocity As Single
  187. Dim m_fPitchVelocity As Single
  188.  
  189. Dim m_fYaw As Single
  190. Dim m_fPitch As Single
  191. Dim m_vPosition As D3DVECTOR
  192.  
  193. Dim m_bKey(256) As Boolean
  194. Dim m_matView As D3DMATRIX
  195. Dim m_matOrientation As D3DMATRIX
  196.  
  197. Dim m_MediaDir As String
  198.  
  199. Const kdx = 256&
  200. Const kdy = 256&
  201.  
  202. Const D3DFVF_VERTEX = D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1
  203.  
  204. Friend Sub Init(hwnd As Long, font As IFontDisp, font2 As IFontDisp)
  205.     Dim i As Long
  206.     
  207.     'Save hwnd
  208.     m_hwnd = hwnd
  209.     
  210.     'convert IFontDisp to Ifont
  211.     Set m_vbfont = font
  212.     Set m_vbfont2 = font2
  213.     
  214.     'initialized d3d
  215.     m_binit = D3DUtil_Init(hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  216.         
  217.     'exit if initialization failed
  218.     If m_binit = False Then End
  219.     
  220.     DeleteDeviceObjects
  221.     InitDeviceObjects
  222.     BuildDefaultDataList
  223.     ComputeDataExtents
  224.     BuildGraph
  225.     RestoreDeviceObjects
  226.     
  227.     DoEvents
  228.     
  229.     m_bRot = True
  230.     m_xHeader = "X Axis"
  231.     m_yHeader = "Y Axis"
  232.     m_zHeader = "Z Axis"
  233.     m_sizeHeader = "s"
  234.     
  235.     m_vPosition = vec3(0, 0, -20)
  236.  
  237.     'Initialze camera matrices
  238.     g_dev.GetTransform D3DTS_VIEW, m_matView
  239.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  240.  
  241.     Timer1.Enabled = True
  242.     Call DXUtil_Timer(TIMER_start)
  243.     
  244. End Sub
  245.  
  246. Private Sub BuildDefaultDataList()
  247.     
  248.     Set m_data = New Collection
  249.     
  250.     Dim i As Single
  251.     
  252.     For i = 1 To 40 Step 2
  253.         AddEntry "pt" + CStr(i), 1 / CSng(i), (i * i) - 25 * i, CSng(i), (0.7 + i / 16), D3DCOLORVALUEtoLONG(ColorValue4(1, 1, 0.5 + i / 20, i / 80)), ""
  254.     Next
  255.     
  256.     m_formatX = "0.000"
  257.     m_formatY = "0.000"
  258.     m_formatZ = "0.000"
  259.     m_formatSize = "0.000"
  260.     m_bConnectlines = True
  261.     m_bHeightLines = True
  262.     m_bShowBase = True
  263.     m_bFootLines = True
  264.     
  265.     m_xHeader = "X Axis"
  266.     m_yHeader = "Y Axis"
  267.     m_zHeader = "Z Axis"
  268.     m_sizeHeader = "s"
  269.     
  270. End Sub
  271.  
  272. Sub RestoreDeviceObjects()
  273.  
  274.     g_lWindowWidth = Me.ScaleWidth
  275.     g_lWindowHeight = Me.ScaleHeight
  276.     D3DUtil_SetupDefaultScene
  277.     
  278.     D3DUtil_SetupCamera vec3(0, 5, -20), vec3(0, 0, 0), vec3(0, 1, 0)
  279.     
  280.     'allow the application to show both sides of all surfaces
  281.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  282.     
  283.     'turn on min filtering since our text is often smaller
  284.     'than original size
  285.     g_dev.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  286.     
  287.     
  288.      Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  289.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  290.         
  291. End Sub
  292.  
  293. Private Sub ComputeDataExtents()
  294.     Dim mind As Single
  295.     Dim maxd As Single
  296.     Dim entry As DataEntry
  297.     
  298.     mind = -9E+20
  299.     maxd = 9E+20
  300.     
  301.     m_maxX = mind:    m_maxY = mind:    m_maxZ = mind:    m_maxsize = mind
  302.     m_minX = maxd:    m_minY = maxd:    m_minZ = maxd:    m_minSize = maxd
  303.    
  304.     
  305.     'Dim entry As DataEntry
  306.     For Each entry In m_data
  307.                         
  308.         If entry.datax > m_maxX Then m_maxX = entry.datax
  309.         If entry.datay > m_maxY Then m_maxY = entry.datay
  310.         If entry.dataz > m_maxZ Then m_maxZ = entry.dataz
  311.         If entry.dataSize > m_maxsize Then m_maxsize = entry.dataSize
  312.         
  313.         If entry.datax < m_minX Then m_minX = entry.datax
  314.         If entry.datay < m_minY Then m_minY = entry.datay
  315.         If entry.dataz < m_minZ Then m_minZ = entry.dataz
  316.         If entry.dataSize < m_minSize Then m_minSize = entry.dataSize
  317.                 
  318.     Next
  319.  
  320.     m_extX = m_maxX - m_minX
  321.     m_extY = m_maxY - m_minY
  322.     m_extZ = m_maxZ - m_minZ
  323.     m_extSize = m_maxsize - m_minSize
  324.     
  325.     Dim kScale As Single
  326.     kScale = 5
  327.     
  328.     m_scalex = 1
  329.     m_scaley = 1
  330.     m_scalez = 1
  331.     m_scalesize = 1
  332.     
  333.     If m_maxX > Abs(m_minX) Then
  334.         If m_maxX <> 0 Then m_scalex = kScale / m_maxX
  335.     Else
  336.         If m_minX <> 0 Then m_scalex = kScale / Abs(m_minX)
  337.     End If
  338.     
  339.     If m_maxY > Abs(m_minY) Then
  340.         If m_maxY <> 0 Then m_scaley = kScale / m_maxY
  341.     Else
  342.         If m_minY <> 0 Then m_scaley = kScale / Abs(m_minY)
  343.     End If
  344.    
  345.  
  346.     If m_maxZ > Abs(m_minZ) Then
  347.         If m_maxZ <> 0 Then m_scalez = kScale / m_maxZ
  348.     Else
  349.         If m_minZ <> 0 Then m_scalez = kScale / Abs(m_minZ)
  350.     End If
  351.     
  352.     
  353.     If m_maxsize = 0 Then m_maxsize = 1
  354.     m_scalesize = 1 * (kScale) / m_maxsize
  355.         
  356.  
  357.     
  358.     'scale graph data to fit
  359.     For Each entry In m_data
  360.                      
  361.         entry.x = entry.datax * m_scalex
  362.         entry.y = entry.datay * m_scaley
  363.         entry.z = entry.dataz * m_scalez
  364.         entry.size = entry.dataSize * m_scalesize
  365.     
  366.     Next
  367.  
  368. End Sub
  369.  
  370. Public Sub AddEntry(sName As String, x As Double, y As Double, z As Double, size As Double, color As Long, data As Variant)
  371.     On Local Error GoTo errOut
  372.     Dim entry As New DataEntry
  373.     entry.dataname = sName
  374.     entry.datax = x
  375.     entry.datay = y
  376.     entry.dataz = z
  377.     entry.dataSize = size
  378.     entry.color = color
  379.     entry.data = data
  380.     m_data.Add entry
  381.     Exit Sub
  382. errOut:
  383.     MsgBox "unable to add entry"
  384. End Sub
  385.  
  386.  
  387. Public Sub DrawGraph()
  388.     Dim entry As DataEntry
  389.     Dim hr As Long
  390.     
  391.     If m_binit = False Then Exit Sub
  392.     
  393.     'See what state the device is in.
  394.     hr = g_dev.TestCooperativeLevel
  395.     If hr = D3DERR_DEVICENOTRESET Then
  396.         g_dev.Reset g_d3dpp
  397.         RestoreDeviceObjects
  398.     ElseIf hr <> 0 Then
  399.         Exit Sub
  400.     End If
  401.     
  402.     m_graphroot.UpdateFrames
  403.              
  404.     'Clear the previous render with the backgroud color
  405.     'We clear to grey but notice that we are using a hexidecimal
  406.     'number to represent Alpha Red Green and blue
  407.     D3DUtil_ClearAll &HFF707070
  408.     
  409.     'set the ambient lighting level
  410.     g_dev.SetRenderState D3DRS_AMBIENT, &HFFC0C0C0
  411.     
  412.     
  413.     g_dev.BeginScene
  414.         
  415.  
  416.         
  417.     
  418.     'only render objects underneath the xzplane
  419.     m_quad1.Enabled = False
  420.     m_quad2.Enabled = True
  421.     m_XZPlaneFrame.Enabled = False
  422.     m_graphroot.Render g_dev
  423.  
  424.     'render the objects in front of xz plane
  425.     m_quad1.Enabled = True
  426.     m_quad2.Enabled = False
  427.     m_XZPlaneFrame.Enabled = False
  428.     m_graphroot.Render g_dev
  429.         
  430.         
  431.         
  432.     DrawLines 0
  433.   
  434.     DrawAxisNameSquare 0    'x axis
  435.     DrawAxisNameSquare 2    'z axis
  436.     
  437.         
  438.     'draw pop up text
  439.     If m_drawtextEnable Then
  440.         g_d3dx.DrawText m_font, &HFF00FFFF, m_drawtext, m_drawtextpos, 0
  441.     End If
  442.     
  443.     Dim rc As RECT
  444.     rc.Top = 20:    rc.Left = 10
  445.     g_d3dx.DrawText m_font, &HFF00FFFF, "Height = " + m_yHeader, rc, 0
  446.     rc.Top = 40:    rc.Left = 10
  447.     g_d3dx.DrawText m_font, &HFF00FFFF, "Size = " + m_sizeHeader, rc, 0
  448.     
  449.     
  450.     
  451.     'render the xzplane with transparency
  452.     If m_bShowBase Then
  453.         m_quad1.Enabled = False
  454.         m_quad2.Enabled = False
  455.         m_XZPlaneFrame.Enabled = True
  456.         m_graphroot.Render g_dev
  457.     End If
  458.     
  459.     g_dev.EndScene
  460.     
  461.     D3DUtil_PresentAll m_hwnd
  462.  
  463. End Sub
  464.  
  465.  
  466.  
  467. Public Sub BuildGraph()
  468.     Dim entry As DataEntry
  469.     Dim material As D3DMATERIAL8
  470.     Dim newFrame As CD3DFrame
  471.     Dim i As Long
  472.     Dim d3ddm As D3DDISPLAYMODE
  473.         
  474.     If m_binit = False Then Exit Sub
  475.     
  476.     
  477.     
  478.     'Create rotatable root object
  479.     Set m_graphroot = D3DUtil_CreateFrame(Nothing)
  480.                 
  481.     'Create XZ plane for reference
  482.     material.diffuse = LONGtoD3DCOLORVALUE(&H6FC0C0C0)
  483.     material.Ambient = material.diffuse
  484.     Set m_XZPlaneFrame = D3DUtil_CreateFrame(m_graphroot)
  485.     m_XZPlaneFrame.AddD3DXMesh(m_meshplane).SetMaterialOverride material
  486.     m_XZPlaneFrame.SetOrientation D3DUtil_RotationAxis(1, 0, 0, 90)
  487.     
  488.     Set m_quad1 = D3DUtil_CreateFrame(m_graphroot)
  489.     Set m_quad2 = D3DUtil_CreateFrame(m_graphroot)
  490.     
  491.     Set m_LabelX = D3DUtil_CreateFrame(m_graphroot)
  492.     m_LabelX.SetPosition vec3(0, 0, -6)
  493.     
  494.     Set m_LabelY = D3DUtil_CreateFrame(Nothing)
  495.     m_LabelY.SetPosition vec3(-8, 8, 0)
  496.     
  497.     
  498.     Set m_LabelZ = D3DUtil_CreateFrame(m_graphroot)
  499.     m_LabelZ.SetPosition vec3(6, 0, 0)
  500.     m_LabelZ.SetOrientation D3DUtil_RotationAxis(0, 1, 0, -90)
  501.     
  502.     
  503.     Dim quadframe As CD3DFrame
  504.     
  505.     For Each entry In m_data
  506.         If entry.y >= 0 Then Set quadframe = m_quad1
  507.         If entry.y < 0 Then Set quadframe = m_quad2
  508.                 
  509.         'Set material of objects
  510.         material.diffuse = LONGtoD3DCOLORVALUE(entry.color)
  511.         material.Ambient = material.diffuse
  512.                 
  513.         'Create individual objects
  514.         Set newFrame = D3DUtil_CreateFrame(quadframe)
  515.         newFrame.SetScale entry.size
  516.         newFrame.SetPosition vec3(entry.x, entry.y, entry.z)
  517.         newFrame.AddD3DXMesh(m_meshobj).SetMaterialOverride material
  518.         i = i + 1
  519.         newFrame.ObjectName = Str(i)
  520.    Next
  521.    
  522.    'Take care of labels
  523.     Dim surf As Direct3DSurface8
  524.     Dim rc As RECT
  525.     Dim rts As D3DXRenderToSurface
  526.     Dim rtsviewport As D3DVIEWPORT8
  527.     
  528.     Set surf = m_Tex.GetSurfaceLevel(0)
  529.   
  530.     rtsviewport.height = kdx
  531.     rtsviewport.width = kdy
  532.     rtsviewport.MaxZ = 1
  533.  
  534.     Call g_dev.GetDisplayMode(d3ddm)
  535.     Set rts = g_d3dx.CreateRenderToSurface(g_dev, kdx, kdy, d3ddm.format, 1, D3DFMT_D16)
  536.   
  537.     rts.BeginScene surf, rtsviewport
  538.     g_dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFFC0C0C0, 1, 0
  539.         
  540.     
  541.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  542.     m_font2height = rc.bottom
  543.     
  544.     
  545.     rc.Top = m_font2height * 0: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  546.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  547.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, 0
  548.     
  549.     rc.Top = m_font2height * 1: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  550.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, DT_CALCRECT
  551.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, 0
  552.     
  553.     rc.Top = m_font2height * 2: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  554.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, DT_CALCRECT
  555.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, 0
  556.     
  557.     rts.EndScene
  558.  
  559.    
  560.    m_bGraphInit = True
  561. End Sub
  562.  
  563.  
  564. Public Sub InitDeviceObjects()
  565.     
  566.     Dim d3ddm As D3DDISPLAYMODE
  567.     
  568.     If m_binit = False Then Exit Sub
  569.     
  570.  
  571.     Dim rc As RECT
  572.     
  573.     Set m_meshobj = g_d3dx.CreateSphere(g_dev, 0.1, 16, 16, Nothing)
  574.     Set m_meshplane = g_d3dx.CreateBox(g_dev, 10, 10, 0.1, Nothing)
  575.     Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  576.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  577.     
  578.     Call g_dev.GetDisplayMode(d3ddm)
  579.     
  580.     'Create Textures
  581.     Set m_Tex = g_d3dx.CreateTexture(g_dev, kdx, kdx, 0, 0, d3ddm.format, D3DPOOL_MANAGED)
  582.     
  583.     
  584.        
  585.     
  586. End Sub
  587.  
  588.  
  589.  
  590. Private Sub DrawLines(quad As Long)
  591.     Dim entry As DataEntry
  592.     Dim vLast As D3DVECTOR, vNext As D3DVECTOR
  593.     Dim vGround As D3DVECTOR
  594.     Dim vGround1 As D3DVECTOR
  595.     Dim vGround2 As D3DVECTOR
  596.     Dim i As Long
  597.     
  598.     'Link lines
  599.     g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
  600.     
  601.     Set entry = m_data.item(1)
  602.     vLast = vec3(entry.x, entry.y, entry.z)
  603.     
  604.     vGround = vLast
  605.     vGround.y = 0
  606.     
  607.     Call DrawLine(vGround, vLast, &HFFFF0000)
  608.     
  609.     For i = 2 To m_data.count
  610.         Set entry = m_data.item(i)
  611.         vNext = vec3(entry.x, entry.y, entry.z)
  612.         
  613.         If m_bConnectlines Then
  614.             Call DrawLine(vLast, vNext, &HFFFF00FF)
  615.         End If
  616.         
  617.         vGround = vNext
  618.         vGround.y = 0
  619.         vGround1 = vGround
  620.         vGround1.y = 0.1
  621.         vGround2 = vLast
  622.         vGround2.y = 0.1
  623.         
  624.         If m_bHeightLines Then
  625.             Call DrawLine(vGround, vNext, &HFFFF0000)
  626.         End If
  627.         
  628.         If m_bFootLines Then
  629.             Call DrawLine(vGround1, vGround2, &HFF10FF30)
  630.         End If
  631.         
  632.         vLast = vNext
  633.     Next
  634.     
  635.     DrawLine vec3(-5, 0.1, 0), vec3(5, 0.1, 0), &HFF0&
  636.     DrawLine vec3(0, 0.1, -5), vec3(0, 0.1, 5), &HFF0&
  637.     
  638. End Sub
  639.  
  640. Private Sub DrawLine(v1 As D3DVECTOR, v2 As D3DVECTOR, color As Long)
  641.     
  642.     Dim mat As D3DMATERIAL8
  643.     mat.diffuse = LONGtoD3DCOLORVALUE(color)
  644.     mat.Ambient = mat.diffuse
  645.     g_dev.SetMaterial mat
  646.     
  647.     Dim dataOut(2) As D3DVERTEX
  648.     LSet dataOut(0) = v1
  649.     LSet dataOut(1) = v2
  650.     g_dev.SetVertexShader D3DFVF_VERTEX
  651.     g_dev.DrawPrimitiveUP D3DPT_LINELIST, 1, dataOut(0), Len(dataOut(0))
  652.     
  653. End Sub
  654.  
  655.  
  656.  
  657. Public Sub MouseOver(Button As Integer, Shift As Integer, x As Single, y As Single)
  658.     
  659.     If m_binit = False Then Exit Sub
  660.     
  661.     Dim pick As New CD3DPick
  662.     Dim frame As CD3DFrame
  663.     Dim nid As Long
  664.     Dim entry As DataEntry
  665.     
  666.     'remove the XZ plane from consideration for pick
  667.     m_XZPlaneFrame.Enabled = False
  668.     m_quad1.Enabled = True
  669.     m_quad2.Enabled = True
  670.     
  671.     
  672.     pick.ViewportPick m_graphroot, x, y
  673.     nid = pick.FindNearest()
  674.     If nid < 0 Then
  675.         m_drawtextEnable = False
  676.         Exit Sub
  677.     End If
  678.         
  679.     Set frame = pick.GetFrame(nid)
  680.     
  681.     'have matrices pre computed for scene graph
  682.     m_graphroot.UpdateFrames
  683.     
  684.     'due some math to get position of item in screen space
  685.     Dim viewport As D3DVIEWPORT8
  686.     Dim projmatrix As D3DMATRIX
  687.     Dim viewmatrix As D3DMATRIX
  688.     Dim vOut As D3DVECTOR
  689.     
  690.     g_dev.GetViewport viewport
  691.     g_dev.GetTransform D3DTS_PROJECTION, projmatrix
  692.     g_dev.GetTransform D3DTS_VIEW, viewmatrix
  693.     D3DXVec3Project vOut, vec3(0, 0, 0), viewport, projmatrix, viewmatrix, frame.GetUpdatedMatrix
  694.             
  695.     Debug.Print vOut.x, vOut.y, frame.ObjectName
  696.     
  697.     
  698.     Dim destRect As RECT
  699.     m_drawtextpos.Left = x - 20
  700.     m_drawtextpos.Top = y - 70
  701.     
  702.     If m_drawtextpos.Left < 0 Then m_drawtextpos.Left = 1
  703.     If m_drawtextpos.Top < 0 Then m_drawtextpos.Top = 1
  704.     
  705.     
  706.     Set entry = m_data.item(val(frame.ObjectName))
  707.     With entry
  708.         m_drawtext = .dataname + Chr(13)
  709.         m_drawtext = m_drawtext + " " + m_xHeader + "=" + format$(.datax, m_formatX) + Chr(13)
  710.         m_drawtext = m_drawtext + " " + m_yHeader + "=" + format$(.datay, m_formatY) + Chr(13)
  711.         m_drawtext = m_drawtext + " " + m_zHeader + "=" + format$(.dataz, m_formatZ) + Chr(13)
  712.         m_drawtext = m_drawtext + " " + m_sizeHeader + "=" + format$(.dataSize, m_formatSize)
  713.     End With
  714.     m_drawtextEnable = True
  715.  
  716. End Sub
  717.  
  718. Sub FrameMove()
  719.  
  720.     'for camera movement
  721.     m_fElapsedTime = DXUtil_Timer(TIMER_GETELLAPSEDTIME) * 1.3
  722.     If m_fElapsedTime < 0 Then Exit Sub
  723.         
  724.         
  725.     If m_bRot And m_bMouseDown = False Then
  726.         m_graphroot.AddRotation COMBINE_BEFORE, 0, 1, 0, (g_pi / 40) * m_fElapsedTime
  727.     End If
  728.         
  729.         
  730.     ' Slow things down for the REF device
  731.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  732.  
  733.     Dim fSpeed As Single
  734.     Dim fAngularSpeed
  735.     
  736.     fSpeed = 5 * m_fElapsedTime
  737.     fAngularSpeed = 1 * m_fElapsedTime
  738.  
  739.     ' Slowdown the camera movement
  740.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  741.     m_fYawVelocity = m_fYawVelocity * 0.9
  742.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  743.  
  744.     ' Process keyboard input
  745.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  746.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  747.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  748.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  749.     
  750.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  751.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  752.     
  753.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  754.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  755.     
  756.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  757.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  758.     
  759.     
  760.  
  761.     ' Update the position vector
  762.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  763.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  764.     D3DXVec3Add vT, vT, vTemp
  765.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  766.     D3DXVec3Add m_vPosition, m_vPosition, vT
  767.     
  768.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  769.  
  770.     ' Update the yaw-pitch-rotation vector
  771.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  772.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  773.     If (m_fPitch < 0) Then m_fPitch = 0
  774.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  775.  
  776.     Dim qR As D3DQUATERNION, det As Single
  777.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  778.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  779.     D3DXMatrixInverse m_matView, det, m_matOrientation
  780.  
  781.         'set new view matrix
  782.     g_dev.SetTransform D3DTS_VIEW, m_matView
  783.  
  784. End Sub
  785.  
  786. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  787.     m_bKey(KeyCode) = True
  788. End Sub
  789.  
  790. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  791.     m_bKey(KeyCode) = False
  792. End Sub
  793.  
  794. Private Sub Form_Load()
  795.     
  796.     'Show the form
  797.     Me.Show
  798.     DoEvents
  799.         
  800.     m_MediaDir = FindMediaDir("ScatterData.csv")
  801.     D3DUtil.D3DUtil_SetMediaPath m_MediaDir
  802.     
  803.     'initialize the graph
  804.     Init Me.hwnd, Me.font, Command1.font
  805.     
  806.     
  807. End Sub
  808.  
  809. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  810.     If Button = 2 Then
  811.         Me.PopupMenu MENU_POPUP
  812.     Else
  813.     
  814.         '- save our current position
  815.         m_bMouseDown = True
  816.         m_lastX = x
  817.         m_lasty = y
  818.         
  819.     End If
  820. End Sub
  821.  
  822. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  823.         
  824.     If m_binit = False Then Exit Sub
  825.     
  826.     If Button = 2 Then Exit Sub
  827.     If m_bMouseDown = False Then
  828.         Call MouseOver(Button, Shift, x, y)
  829.     Else
  830.         '- Rotate the object
  831.         RotateTrackBall CInt(x), CInt(y)
  832.     End If
  833.     
  834.     FrameMove
  835.     DrawGraph
  836.     
  837. End Sub
  838.  
  839. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  840.     m_bMouseDown = False
  841. End Sub
  842.  
  843.  
  844. '-----------------------------------------------------------------------------
  845. ' Name: Form_Resize()
  846. ' Desc: hadle resizing of the D3D backbuffer
  847. '-----------------------------------------------------------------------------
  848. Private Sub Form_Resize()
  849.     
  850.     
  851.     Timer1.Enabled = False
  852.     
  853.     ' If D3D is not initialized then exit
  854.     If Not m_binit Then Exit Sub
  855.     
  856.     ' If we are in a minimized state stop the timer and exit
  857.     If Me.WindowState = vbMinimized Then
  858.         DXUtil_Timer TIMER_STOP
  859.         m_bMinimized = True
  860.         Exit Sub
  861.         
  862.     ' If we just went from a minimized state to maximized
  863.     ' restart the timer
  864.     Else
  865.         If m_bMinimized = True Then
  866.             DXUtil_Timer TIMER_start
  867.             m_bMinimized = False
  868.         End If
  869.     End If
  870.     
  871.     ' Dont let the window get too small
  872.     If Me.ScaleWidth < 10 Then
  873.         Me.width = Screen.TwipsPerPixelX * 10
  874.         Exit Sub
  875.     End If
  876.     
  877.     If Me.ScaleHeight < 10 Then
  878.         Me.height = Screen.TwipsPerPixelY * 10
  879.         Exit Sub
  880.     End If
  881.      
  882.     DeleteDeviceObjects
  883.  
  884.     'reset and resize our D3D backbuffer to the size of the window
  885.     D3DUtil_ResizeWindowed Me.hwnd
  886.     
  887.     'All state get losts after a reset so we need to reinitialze it here
  888.     RestoreDeviceObjects
  889.     
  890.     Timer1.Enabled = True
  891.     
  892. End Sub
  893.  
  894. '- Rotate Track ball
  895. '  given a point on the screen the mouse was moved to
  896. '  simulate a track ball
  897. Private Sub RotateTrackBall(x As Integer, y As Integer)
  898.  
  899.     
  900.     Dim delta_x As Single, delta_y As Single
  901.     Dim delta_r As Single, radius As Single, denom As Single, angle As Single
  902.     
  903.     ' rotation axis in camcoords, worldcoords, sframecoords
  904.     Dim axisC As D3DVECTOR
  905.     Dim wc As D3DVECTOR
  906.     Dim axisS As D3DVECTOR
  907.     Dim base As D3DVECTOR
  908.     Dim origin As D3DVECTOR
  909.     
  910.     delta_x = x - m_lastX
  911.     delta_y = y - m_lasty
  912.     m_lastX = x
  913.     m_lasty = y
  914.  
  915.             
  916.      delta_r = Sqr(delta_x * delta_x + delta_y * delta_y)
  917.      radius = 50
  918.      denom = Sqr(radius * radius + delta_r * delta_r)
  919.     
  920.     If (delta_r = 0 Or denom = 0) Then Exit Sub
  921.     angle = (delta_r / denom)
  922.  
  923.     axisC.x = (-delta_y / delta_r)
  924.     axisC.y = (-delta_x / delta_r)
  925.     axisC.z = 0
  926.  
  927.  
  928.     'transform camera space vector to world space
  929.     'm_largewindow.m_cameraFrame.Transform wc, axisC
  930.     g_dev.GetTransform D3DTS_VIEW, g_viewMatrix
  931.     D3DXVec3TransformCoord wc, axisC, g_viewMatrix
  932.     
  933.     
  934.     'transform world space vector into Model space
  935.     m_graphroot.UpdateFrames
  936.     axisS = m_graphroot.InverseTransformCoord(wc)
  937.         
  938.     'transform origen camera space to world coordinates
  939.     'm_largewindow.m_cameraFrame.Transform  wc, origin
  940.     D3DXVec3TransformCoord wc, origin, g_viewMatrix
  941.     
  942.     'transfer cam space origen to model space
  943.     base = m_graphroot.InverseTransformCoord(wc)
  944.     
  945.     axisS.x = axisS.x - base.x
  946.     axisS.y = axisS.y - base.y
  947.     axisS.z = axisS.z - base.z
  948.     
  949.     m_graphroot.AddRotation COMBINE_BEFORE, axisS.x, axisS.y, axisS.z, angle
  950.     
  951. End Sub
  952.  
  953.  
  954. Private Sub Form_Paint()
  955.     If Not m_binit Then Exit Sub
  956.     If Not m_bGraphInit Then Exit Sub
  957.     DrawGraph
  958. End Sub
  959.  
  960. Private Sub Form_Unload(Cancel As Integer)
  961.     End
  962. End Sub
  963.  
  964. Private Sub MENU_BASE_Click()
  965.     m_bShowBase = Not m_bShowBase
  966.     MENU_BASE.Checked = m_bShowBase
  967. End Sub
  968.  
  969. Private Sub MENU_CONNECT_Click()
  970.     m_bConnectlines = Not m_bConnectlines
  971.     MENU_CONNECT.Checked = m_bConnectlines
  972. End Sub
  973.  
  974. Private Sub MENU_FOOTLINES_Click()
  975.     m_bFootLines = Not m_bFootLines
  976.     MENU_FOOTLINES.Checked = m_bFootLines
  977. End Sub
  978.  
  979. Private Sub MENU_LINES_Click()
  980.     m_bHeightLines = Not m_bHeightLines
  981.     MENU_LINES.Checked = m_bHeightLines
  982. End Sub
  983.  
  984. Private Sub MENU_LOAD_Click()
  985.     Dim sFile As String
  986.     
  987.     CommonDialog1.FileName = ""
  988.     CommonDialog1.DefaultExt = "csv"
  989.     CommonDialog1.filter = "csv|*.csv"
  990.     CommonDialog1.InitDir = m_MediaDir
  991.     
  992.     On Local Error Resume Next
  993.     CommonDialog1.ShowOpen
  994.     sFile = CommonDialog1.FileName
  995.     If sFile = "" Then Exit Sub
  996.     LoadFile sFile
  997.     
  998.     Set m_graphroot = Nothing
  999.     Set m_quad1 = Nothing
  1000.     Set m_quad2 = Nothing
  1001.     Set m_XZPlaneFrame = Nothing
  1002.     
  1003.     ComputeDataExtents
  1004.     BuildGraph
  1005.     RestoreDeviceObjects
  1006.     
  1007. End Sub
  1008.  
  1009. Private Sub MENU_RESET_Click()
  1010.     m_graphroot.SetMatrix g_identityMatrix
  1011.     m_vPosition = vec3(0, 0, -20)
  1012.     m_fYaw = 0
  1013.     m_fPitch = 0
  1014.  
  1015.     Call D3DXMatrixTranslation(m_matOrientation, 0, 0, 0)
  1016. End Sub
  1017.  
  1018. Private Sub MENU_ROTATE_Click()
  1019.     m_bRot = Not m_bRot
  1020.     MENU_ROTATE.Checked = m_bRot
  1021. End Sub
  1022.  
  1023. Private Sub Timer1_Timer()
  1024.     If Not m_binit Then Exit Sub
  1025.     
  1026.     FrameMove
  1027.     DrawGraph
  1028. End Sub
  1029.  
  1030. Sub LoadFile(sFile As String)
  1031.     
  1032.     
  1033.     If Dir$(sFile) = "" Then
  1034.         MsgBox "Unable to find " + sFile
  1035.         Exit Sub
  1036.     End If
  1037.     
  1038.     Dim fl As Long
  1039.     Dim strIn As String
  1040.     Dim strTrim As String
  1041.     Dim strFirstChar As String
  1042.     Dim splitArray
  1043.     Dim cols As Long
  1044.     Dim bFoundData As Boolean
  1045.     Dim sName As String
  1046.     Dim x As Double
  1047.     Dim y As Double
  1048.     Dim z As Double
  1049.     Dim size As Double
  1050.     Dim color As Long
  1051.     Dim data
  1052.     Dim i As Long
  1053.     Dim olddata As Collection
  1054.     
  1055.     fl = FreeFile
  1056.     
  1057.     On Local Error GoTo errOut
  1058.     
  1059.     Set olddata = m_data
  1060.     Set m_data = New Collection
  1061.     
  1062.     Open sFile For Input As fl
  1063.         
  1064.     Do While Not EOF(fl)
  1065.         Line Input #fl, strIn
  1066.         strTrim = Trim(strIn)
  1067.         
  1068.         'skip comment lines
  1069.         strFirstChar = Mid$(strTrim, 1, 1)
  1070.         If strFirstChar = "#" Or strFirstChar = ";" Then GoTo nextLine
  1071.         If strTrim = "" Then GoTo nextLine
  1072.         
  1073.         splitArray = Split(strTrim, ",")
  1074.         
  1075.         cols = UBound(splitArray)
  1076.         If cols < 4 Then
  1077.             MsgBox "Comma delimited file must have at least 4 columns (name,x,y,z)"
  1078.             Exit Sub
  1079.         End If
  1080.                 
  1081.         
  1082.         'If we have not found numbers see if we found a header row
  1083.         If Not bFoundData Then
  1084.             If IsNumeric(splitArray(1)) = False Then
  1085.                 'assume data is a header row
  1086.                 m_xHeader = CStr(splitArray(1))
  1087.                 m_yHeader = CStr(splitArray(2))
  1088.                 m_zHeader = CStr(splitArray(3))
  1089.                 m_sizeHeader = CStr(splitArray(4))
  1090.                 GoTo nextLine
  1091.             Else
  1092.                 bFoundData = True
  1093.             End If
  1094.         End If
  1095.         
  1096.         sName = CStr(splitArray(0))
  1097.         x = val(splitArray(1))
  1098.         y = val(splitArray(2))
  1099.         z = val(splitArray(3))
  1100.         
  1101.         'set defaults
  1102.         i = i + 1
  1103.         size = 1
  1104.         color = D3DCOLORVALUEtoLONG(ColorValue4(1, (10 + i Mod 20) / 30, 0.3, (10 + (i Mod 40)) / 50))
  1105.         data = ""
  1106.         
  1107.         If cols >= 4 Then size = val(splitArray(4))
  1108.         If cols >= 5 Then color = val(splitArray(5))
  1109.         If cols >= 6 Then data = splitArray(6)
  1110.         
  1111.         AddEntry sName, x, y, z, size, color, data
  1112.         
  1113.         
  1114. nextLine:
  1115.     Loop
  1116.     
  1117.     Set olddata = Nothing
  1118.     Close fl
  1119.     Exit Sub
  1120.     
  1121. errOut:
  1122.     Set m_data = olddata
  1123.     MsgBox "there was an error loading " + sFile
  1124.     Close fl
  1125. End Sub
  1126.  
  1127. Sub DrawAxisNameSquare(i As Long)
  1128.  
  1129.     Dim verts(4) As D3DVERTEX
  1130.     Dim w As Single
  1131.     Dim h As Single
  1132.     Dim mat As D3DMATERIAL8
  1133.     Dim sv As Single
  1134.     Dim ev As Single
  1135.     
  1136.     
  1137.     w = 2:    h = 0.25
  1138.         
  1139.     
  1140.  
  1141.     mat.Ambient = ColorValue4(1, 1, 1, 1)
  1142.     mat.diffuse = ColorValue4(1, 1, 1, 1)
  1143.     
  1144.     
  1145.     
  1146.     sv = (m_font2height * (i) / kdy)
  1147.     ev = (m_font2height * (i + 1) / kdy)
  1148.     
  1149.  
  1150.     Select Case i
  1151.         Case 0
  1152.             g_dev.SetTransform D3DTS_WORLD, m_LabelX.GetUpdatedMatrix
  1153.             
  1154.         Case 1
  1155.             'Y axis now part of HUD
  1156.             Exit Sub
  1157.         Case 2
  1158.             g_dev.SetTransform D3DTS_WORLD, m_LabelZ.GetUpdatedMatrix
  1159.             
  1160.     End Select
  1161.         
  1162.     g_dev.SetTexture 0, m_Tex
  1163.     g_dev.SetMaterial mat
  1164.     
  1165.     With verts(0): .x = -w: .y = -h: .tu = 0: .tv = ev: .nz = -1: End With
  1166.     With verts(1): .x = w: .y = -h: .tu = 1: .tv = ev: .nz = -1: End With
  1167.     With verts(2): .x = w: .y = h: .tu = 1: .tv = sv: .nz = -1: End With
  1168.     With verts(3): .x = -w: .y = h: .tu = 0: .tv = sv: .nz = -1: End With
  1169.     g_dev.SetVertexShader D3DFVF_VERTEX
  1170.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  1171.     
  1172.     
  1173.     With verts(0): .z = 0.01: .x = w: .y = -h: .tu = 0: .tv = ev: .nz = 1: End With
  1174.     With verts(1): .z = 0.01: .x = -w: .y = -h: .tu = 1: .tv = ev: .nz = 1: End With
  1175.     With verts(2): .z = 0.01: .x = -w: .y = h: .tu = 1: .tv = sv: .nz = 1: End With
  1176.     With verts(3): .z = 0.01: .x = w: .y = h: .tu = 0: .tv = sv: .nz = 1: End With
  1177.     g_dev.SetVertexShader D3DFVF_VERTEX
  1178.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  1179.     
  1180.  
  1181. End Sub
  1182.  
  1183. Sub DeleteDeviceObjects()
  1184.     Set m_font = Nothing
  1185.     Set m_font2 = Nothing
  1186. End Sub
  1187.  
  1188.